import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Divider} from 'antd';
import {FILES_API} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
import {injectIntl} from './intl';
import {saleOrderPriceTerms, paymentType, currency, saleOrderTransportType} from '../Enum';
import {conventEnumValueToString, formatAmount} from '../utils';
import {Link} from 'react-router-dom';
const {Description} = DescriptionList;

export class DetailPanel extends React.PureComponent {
    componentDidMount() {
        this.props.init();
    }

    render() {
        const {formatMessage} = this.props.intl;
        const detailData = this.props.detail || {};
        const {declareAttachments = [], clearanceAttachments = []} = detailData;

        return (
            <div className="detail">
                <Card loading={this.props.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'detailPanel.text.theme',
                        defaultMessage: '销售确认书主单'
                    })}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.code',
                            defaultMessage: '销售确认书号'
                        })}>{detailData.code}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandName',
                            defaultMessage: '品牌'
                        })}>{detailData.brandName}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealer',
                            defaultMessage: '总代'
                        })}>{`(${detailData.dealerCode})${detailData.dealerName}`}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.tradeMethodName',
                            defaultMessage: '贸易方式'
                        })}>{detailData.tradeMethodName}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.salesOrderPriceTerms',
                            defaultMessage: '价格条款'
                        })}>{conventEnumValueToString(saleOrderPriceTerms, detailData.salesOrderPriceTerms)}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.payment',
                            defaultMessage: '付款方式'
                        })}>{conventEnumValueToString(paymentType, detailData.payment)}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.letterOfCreditCode',
                            defaultMessage: '信用证号'
                        })}>{detailData.letterOfCreditCode}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.LcVersionCode',
                            defaultMessage: '信用证版本号'
                        })}>
                            <Link key="letterOfCredit" to={routes.letterOfCredit.format(detailData.letterOfCreditId)}>
                                {detailData.LcVersionCode}</Link>
                        </Description>
                        {(detailData.salesOrderPriceTerms === 2 || detailData.salesOrderPriceTerms === 3 ||
                            detailData.salesOrderPriceTerms === 5 || detailData.salesOrderPriceTerms === 6) &&
                        <Description term={formatMessage({
                            id: 'detailPanel.text.shipmentAmount',
                            defaultMessage: '运费'
                        })}>{formatAmount(detailData.shipmentAmount)}
                        </Description>
                        }
                        {(detailData.salesOrderPriceTerms === 3 || detailData.salesOrderPriceTerms === 6) &&
                        <Description term={formatMessage({
                            id: 'detailPanel.text.insuranceAmount',
                            defaultMessage: '保费'
                        })}>{formatAmount(detailData.insuranceAmount)}
                        </Description>
                        }
                        <Description term={formatMessage({
                            id: 'detailPanel.text.totalAmount',
                            defaultMessage: '总金额'
                        })}>{formatAmount(detailData.totalAmount)}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.currency',
                            defaultMessage: '币种'
                        })}>{conventEnumValueToString(currency, detailData.currency)}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.transportType',
                            defaultMessage: '运输方式'
                        })}>{conventEnumValueToString(saleOrderTransportType, detailData.transportType)}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.shippingPortName',
                            defaultMessage: '发运港口'
                        })}>{detailData.shippingPortName}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.destinationPort',
                            defaultMessage: '目的港口'
                        })}>{detailData.destinationPort}
                        </Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.batchNumber',
                            defaultMessage: '批次号'
                        })}>{detailData.batchNumber}
                        </Description>
                    </DescriptionList>
                    <Divider/>
                    {declareAttachments && declareAttachments.length > 0
                        ? <DescriptionList title={formatMessage({
                            id: 'detailPage.card.declareAttachment',
                            defaultMessage: '报关附件清单'
                        })} size="large">
                            {declareAttachments.map(item => (<Description key={item.fileId}>
                                <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">
                                    {item.fileName}</a></Description>))}
                        </DescriptionList>
                        : formatMessage({
                            id: 'detailPage.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}
                    <Divider/>
                    {clearanceAttachments && clearanceAttachments.length > 0
                        ? <DescriptionList title={formatMessage({
                            id: 'detailPage.card.clearanceAttachment',
                            defaultMessage: '清关附件清单'
                        })} size="large">
                            {clearanceAttachments.map(item => (<Description key={item.fileId}>
                                <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">
                                    {item.fileName}</a></Description>))}
                        </DescriptionList>
                        : formatMessage({
                            id: 'detailPage.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    getDetail,
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);
const getIsDealerUser = selectorFactory(['page', 'domainData', 'initData', 'isDealerUser']);

const mapStateToProps = (state, props) => ({
    id: props.id,
    isDealerUser: getIsDealerUser(state),
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    detail: getDetailData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
